package com.spic.business.blast.controller;

import com.spic.business.blast.domain.BlastAlarm;
import com.spic.business.blast.domain.vo.BlastCountVo;
import com.spic.business.blast.service.IBlastAlarmService;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 爆区布防告警Controller
 *
 * @author spic
 * @date 2022-03-25
 */
@RestController
@RequestMapping("/blastAlarm")
public class BlastAlarmController extends BaseController
{
    @Autowired
    private IBlastAlarmService blastAlarmService;

    /**
     * 查询爆区布防告警列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BlastAlarm blastAlarm)
    {
        // @TODO 后期告警太多，数据量太大，可能会查询失败
        List<BlastAlarm> list = blastAlarmService.selectBlastAlarmList(blastAlarm);
        Map<String, List<BlastAlarm>> collect = list.stream().collect(Collectors.groupingBy(BlastAlarm::getBlastTaskId));
        Set<String> sets = collect.keySet();
        Iterator<String> iterator = sets.iterator();
        List<BlastCountVo> blastList = new ArrayList<>();
        while (iterator.hasNext())
        {
            String key = iterator.next();
            List<BlastAlarm> blastAlarms = collect.get(key);
            BlastCountVo vo = new BlastCountVo();
            vo.setTaskId(key);
            vo.setTaskName( (blastAlarms.get(0) != null) ? blastAlarms.get(0).getBlastTaskName() : "");
            vo.setCarNum(blastAlarms.stream().filter(ba -> "1".equals(ba.getObjType())).count());
            vo.setPersonNum(blastAlarms.stream().filter(ba -> "0".equals(ba.getObjType())).count());
            vo.setBlastAlarms(blastAlarms);
            blastList.add(vo);
        }
        return new TableDataInfo(blastList, (int) blastList.stream().count());
    }

    /**
     * ioc查询爆区布防告警列表
     */
    @GetMapping("/lists")
    public AjaxResult lists(BlastAlarm blastAlarm)
    {
        List<BlastAlarm> list = blastAlarmService.selectBlastAlarmList(blastAlarm);
        Map<String,Object> map  = new HashMap<>();
        map.put("list",list);
        map.put("total",list.size());
        return AjaxResult.success(map);
    }

    @GetMapping("alarmCount")
    public int getAlarmNum(String taskId)
    {
        BlastAlarm blastAlarm = new BlastAlarm();
        blastAlarm.setBlastTaskId(taskId);
        List<BlastAlarm> list = blastAlarmService.selectBlastAlarmList(blastAlarm);
        return list.size();
    }

    /**
     * 导出爆区布防告警列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlastAlarm blastAlarm) throws IOException
    {
        List<BlastAlarm> list = blastAlarmService.selectBlastAlarmList(blastAlarm);
        ExcelUtil<BlastAlarm> util = new ExcelUtil<BlastAlarm>(BlastAlarm.class);
        util.exportExcel(response, list, "爆区布防告警数据");
    }

    /**
     * 删除爆区布防告警
     */
	@DeleteMapping("/{alarmIds}")
    public AjaxResult remove(@PathVariable Long[] alarmIds)
    {
        return toAjax(blastAlarmService.deleteBlastAlarmByAlarmIds(alarmIds));
    }
}
